package tw101.bm;

import java.util.ArrayList;

/**
 * Created by Alan on 12/29/13.
 */
public class FindPrimeFactors {

    public static void main (String[] args) {
      for (int i=0; i<100; i++) {
            printFactors(i);
        }
    }

    private static void printFactors (int n) {
        ArrayList<Integer> factors = generate(n);
        //ArrayList<Integer> factors = generateWithDuplicates(n);
        System.out.println(n + ": " + factors);
    }

    private static ArrayList<Integer> generate(int n) {
        ArrayList<Integer> factorList = new ArrayList<Integer>();
        int remainder = n;

        for (int trial=2; trial<=remainder; trial++) {
            if (remainder%trial == 0) {
                while (remainder%trial == 0) {
                    remainder /= trial;
                }
                factorList.add(trial);
            }
        }
        return factorList;
    }

    private static ArrayList<Integer> generateWithDuplicates(int n) {
        ArrayList<Integer> factorList = new ArrayList<Integer>();
        int remainder = n;

        for (int trial=2; trial<=remainder; trial++) {
            if (remainder%trial == 0) {
                while (remainder%trial == 0) {
                    remainder /= trial;
                    factorList.add(trial);
                }
            }
        }
        return factorList;
    }
}
